package com.lz.collection.specification;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * List<Object> List<?> List< ? extends XX> List<? super xx>
 *
 * @author: li
 * @date: 2019/1/10
 */
public class ListTest {
    @Test
    public void test01() {
        List list = new ArrayList();
        list.add(new Object());
        list.add(new String("haha"));
        list.add(new Integer(1));

        List<Object> list1 = new ArrayList();
        list1.add(new Object());
        list1.add(new String("haha"));
        list1.add(Integer.valueOf(12));

        List<?> list2 = new ArrayList();
        list2 = list;
        list2.forEach(System.out::println);
        //  list2.add(new Object());
        // list2.add(new String());
        // list2.add( Integer.valueOf(12));

        List<? extends Pair> list3 = new ArrayList() {

        };

        List<? super Fruit> list4 = new ArrayList();

        list4.add(new Apple());
        list4.add(new Pair());

        String s = "wofuck0909wofuck0909卧槽";
        String s2 = "wofuck0909wofuck0909卧槽";

        long t1 = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            boolean equals = s.equals(s2);
        }
        long t2 = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            s.hashCode();
            s2.hashCode();
            boolean b = s == s2;
        }
        long t3 = System.currentTimeMillis();
        boolean equals = Objects.equals("", new String(""));
        System.out.println(equals);
        System.out.println((t2 - t1) + "==" + (t3 - t2));
    }

    /**
     * fail-fast
     */
    @Test
    public void listConcurrence() {
        List masterList = new ArrayList();
        masterList.add(" one ");
        masterList.add(" two");
        masterList.add(" three");
        masterList.add(" four ");
        masterList.add(" five");

        // subList内部List 指向rootList 此时子表 主表都不能修改
        List branchList = masterList.subList(0, 3);


        masterList.remove(0);
        masterList.add(" ten");
        masterList.clear();
        // 此处不能修改
        branchList.clear();
        branchList.add(" six ");
        branchList.add(" seven");
        branchList.remove(0);

        for (Object t : branchList) {
            System.out.println(t);
            System.out.println(masterList);
        }


    }

    /**
     * foreach() 遍历 remove
     */
    @Test
    public void listConcurrence2() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        for (String s : list                 ) {
            if (s.equals("1")) {
                list.remove(s);
            }
        }
        System.out.println(list);
    }
}

class Eat {
}

class Fruit extends Eat {
}


class Grain extends Eat {
}

class Apple extends Fruit {
}

class Pair extends Fruit {
}

class DoublePair extends Pair {
}


